package com.meituan.catering.management.order.api.http.controller;

import com.meituan.catering.management.common.exception.BizException;
import com.meituan.catering.management.common.helper.StatusHelper;
import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.common.model.converter.UserContextBOConverter;
import com.meituan.catering.management.order.api.http.model.request.*;
import com.meituan.catering.management.order.api.http.model.response.CateringOrderDetailHttpResponse;
import com.meituan.catering.management.order.api.http.model.response.CateringOrderPageHttpResponse;
import com.meituan.catering.management.order.biz.model.CateringOrderDetailBO;
import com.meituan.catering.management.order.biz.model.converter.*;
import com.meituan.catering.management.order.biz.model.request.*;
import com.meituan.catering.management.order.biz.service.CateringOrderBizService;
import com.meituan.catering.management.order.biz.service.CateringOrderQueryService;
import com.meituan.catering.management.order.biz.validator.OrderControllerServiceValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 订单交易Http API
 */
@Api(tags = "订单交易")
@RestController
@RequestMapping("/order/catering")
@Slf4j
public class CateringOrderController {

    @Resource
    private OrderControllerServiceValidator orderControllerServiceValidator;

    @Resource
    private CateringOrderQueryService cateringOrderQueryService;

    @Resource
    private CateringOrderBizService cateringOrderBizService;

    @ApiOperation("分页搜索订单的概要信息列表")
    @PostMapping("/search")
    public CateringOrderPageHttpResponse searchForPage(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("搜索条件") @Valid @RequestBody SearchCateringOrderHttpRequest request) {
        CateringOrderPageHttpResponse response = new CateringOrderPageHttpResponse();
        try {
            orderControllerServiceValidator.searchForPageValid(tenantId, userId, request);
            SearchCateringOrderBizRequest searchCateringOrderBizRequest = SearchCateringOrderBizRequestConverter.toSearchCateringOrderBizRequest(request);
            PageBO pageBO = cateringOrderQueryService.searchCateringOrder(UserContextBOConverter.fromHttpRequest(tenantId, userId), searchCateringOrderBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(CateringOrderPageHttpDTOCOnverter.toCateringOrderPageHttpDTO(pageBO));
        } catch (BizException e) {
            log.error("分页搜索发生异常:tenantId:{},userId:{},request:{}", tenantId, userId, request, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("查看单个订单详情")
    @GetMapping("/{orderId}")
    public CateringOrderDetailHttpResponse findById(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("订单ID") @PathVariable Long orderId) {
        CateringOrderDetailHttpResponse response = new CateringOrderDetailHttpResponse();
        try {
            orderControllerServiceValidator.findByIdValid(tenantId, userId, orderId);
            CateringOrderDetailBO orderDetailBO = cateringOrderQueryService.findById(UserContextBOConverter.fromHttpRequest(tenantId, userId), orderId);
            response.setStatus(StatusHelper.success());
            response.setData(CateringOrderDetailDTOConverter.toCateringOrderDetailHttpDTO(orderDetailBO));
        } catch (BizException e) {
            log.error("查看单个订单发生异常:tenantId:{},userId:{},orderId:{}", tenantId, userId, orderId, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("创建新订单")
    @PostMapping
    public CateringOrderDetailHttpResponse place(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("下单信息") @Valid @RequestBody PlaceCateringOrderHttpRequest request) {
        CateringOrderDetailHttpResponse response = new CateringOrderDetailHttpResponse();
        try {
            orderControllerServiceValidator.placeValid(tenantId, userId, request);
            PlaceCateringOrderBizRequest placeCateringOrderBizRequest = PlaceCateringOrderBizRequestConverter.toPlaceCateringOrderBizRequest(request);
            CateringOrderDetailBO cateringOrderDetailBO = cateringOrderBizService.place(UserContextBOConverter.fromHttpRequest(tenantId, userId), placeCateringOrderBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(CateringOrderDetailDTOConverter.toCateringOrderDetailHttpDTO(cateringOrderDetailBO));
        } catch (BizException e) {
            log.error("创建新订单:tenantId:{},userId:{},request:{}", tenantId, userId, request, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("针对一个订单进行制作")
    @PostMapping("/{orderId}/prepare")
    public CateringOrderDetailHttpResponse prepare(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("订单ID") @PathVariable Long orderId,
            @ApiParam("制作信息") @Valid @RequestBody PrepareCateringOrderHttpRequest request) {
        CateringOrderDetailHttpResponse response = new CateringOrderDetailHttpResponse();
        try {
            orderControllerServiceValidator.prepareValid(tenantId, userId, orderId, request);
            PrepareCateringOrderBizRequest prepareCateringOrderBizRequest = PrepareCateringOrderBizRequestConverter.toPrepareCateringOrderBizRequest(request);
            CateringOrderDetailBO prepare = cateringOrderBizService.prepare(UserContextBOConverter.fromHttpRequest(tenantId, userId), orderId, prepareCateringOrderBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(CateringOrderDetailDTOConverter.toCateringOrderDetailHttpDTO(prepare));
        } catch (BizException e) {
            log.error("订单制作发生异常:tenantId:{},userId:{},request:{},orderId:{}", tenantId, userId, request, orderId, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("针对一个订单进行出餐")
    @PostMapping("/{orderId}/produce")
    public CateringOrderDetailHttpResponse produce(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("订单ID") @PathVariable Long orderId,
            @ApiParam("出餐信息") @Valid @RequestBody ProduceCateringOrderHttpRequest request) {
        CateringOrderDetailHttpResponse response = new CateringOrderDetailHttpResponse();
        try {
            orderControllerServiceValidator.produceValid(tenantId, userId, orderId, request);
            ProduceCateringOrderBizRequest produceCateringOrderBizRequest = ProduceCateringOrderBizRequestConverter.toProduceCateringOrderBizRequest(request);
            CateringOrderDetailBO cateringOrderDetailBO = cateringOrderBizService.produce(UserContextBOConverter.fromHttpRequest(tenantId, userId), orderId, produceCateringOrderBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(CateringOrderDetailDTOConverter.toCateringOrderDetailHttpDTO(cateringOrderDetailBO));
        } catch (BizException e) {
            log.error("订单出餐发生异常:tenantId:{},userId:{},request:{},orderId:{}", tenantId, userId, request, orderId, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("针对一个订单进行加退菜")
    @PostMapping("/{orderId}/adjust")
    public CateringOrderDetailHttpResponse adjust(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("订单ID") @PathVariable Long orderId,
            @ApiParam("加退菜信息") @Valid @RequestBody AdjustCateringOrderHttpRequest request) {
        CateringOrderDetailHttpResponse response = new CateringOrderDetailHttpResponse();
        try {
            orderControllerServiceValidator.adjustValid(tenantId, userId, orderId, request);
            AdjustCateringOrderBizRequest adjustCateringOrderBizRequest = AdjustCateringOrderBizRequestConverter.toAdjustCateringOrderBizRequest(request);
            CateringOrderDetailBO cateringOrderDetailBO = cateringOrderBizService.adjust(UserContextBOConverter.fromHttpRequest(tenantId, userId), orderId, adjustCateringOrderBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(CateringOrderDetailDTOConverter.toCateringOrderDetailHttpDTO(cateringOrderDetailBO));
        } catch (BizException e) {
            log.error("加退菜发生异常:tenantId:{},userId:{},request:{},orderId:{}", tenantId, userId, request, orderId, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("针对一个订单进行结账")
    @PostMapping("/{orderId}/bill")
    public CateringOrderDetailHttpResponse bill(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("订单ID") @PathVariable Long orderId,
            @ApiParam("结账信息") @Valid @RequestBody BillCateringOrderHttpRequest request) {
        CateringOrderDetailHttpResponse response = new CateringOrderDetailHttpResponse();
        try {
            orderControllerServiceValidator.billValid(tenantId, userId, orderId, request);
            BillCateringOrderBizRequest billCateringOrderBizRequest = BillCateringOrderBizRequestConverter.toBillCateringOrderBizRequest(request);
            CateringOrderDetailBO orderDetailBO = cateringOrderBizService.bill(UserContextBOConverter.fromHttpRequest(tenantId, userId), orderId, billCateringOrderBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(CateringOrderDetailDTOConverter.toCateringOrderDetailHttpDTO(orderDetailBO));
        } catch (BizException e) {
            log.error("订单结账发生异常:tenantId:{},userId:{},request:{},orderId:{}", tenantId, userId, request, orderId, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }
}
